﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using CommonWFLibrary;

namespace CommonWFLibraryTest
{
    /// <summary>
    /// Summary description for t_LongRunningActivityPersister
    /// </summary>
    [TestClass]
    public class t_LongRunningActivityPersister
    {
        public t_LongRunningActivityPersister()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [Serializable]
        class bogus
        {
            public int _i1;
            public string _duh;
        }

        [TestMethod]
        public void TestBasicIO()
        {
            FileInfo f = new FileInfo(".\\tempdir\\binaryiodata");
            f.Directory.Create();
            Assert.IsFalse(f.Exists);
            LongRunningActivityBinaryPersister bs = new LongRunningActivityBinaryPersister(f);

            f.Refresh();
            Assert.IsTrue(f.Directory.GetFiles().Length == 0, "Cache output directory is not empty!");
            bogus bzero = bs.Restore() as bogus;
            Assert.IsNull(bzero, "A non-readback should have given us a null");

            bogus b = new bogus();
            b._i1 = 10;
            b._duh = "hi there";

            bs.Save(b);

            f.Refresh();
            Assert.IsTrue(f.Exists, "Output binary file seems to be missing");
            Assert.IsTrue(f.Directory.GetFiles().Length == 1, "Extra files in the output - huh?");

            bogus bback = bs.Restore() as bogus;
            Assert.IsNotNull(bback, "Null item came back!");
            Assert.IsTrue(bback._duh == b._duh, "The string wasn't cached correctly");
            Assert.IsTrue(bback._i1 == b._i1, "The integer wasn't cached correctly");

        }

        [TestMethod]
        public void TestNull()
        {
            /// If we write out a "null" object, make sure the file is deleted.

            FileInfo f = new FileInfo(".\\testnull");
            LongRunningActivityBinaryPersister bs = new LongRunningActivityBinaryPersister(f);

            bogus zero = new bogus();
            zero._i1 = 10;
            bs.Save(zero);

            f.Refresh();
            Assert.IsTrue(f.Exists, "The cache file should be there");
            bs.Save(null);
            f.Refresh();
            Assert.IsFalse(f.Exists, "The cache file should no longer be there!");
        }
    }
}
